package com.hxl.pauserecord.activity;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.hxl.pauserecord.R;
import com.hxl.pauserecord.record.AudioRecorder;

import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends Activity implements View.OnClickListener {
    Button start;
    Button pause;
    Button pcmList;
    Button wavList;

    MainActivity mActivity;
    AudioRecorder audioRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        addListener();
    }

    private void addListener() {
        start.setOnClickListener(this);
        pause.setOnClickListener(this);
        pcmList.setOnClickListener(this);
        wavList.setOnClickListener(this);
    }

    private void init() {
        start = (Button) findViewById(R.id.start);
        pause = (Button) findViewById(R.id.pause);
        pcmList = (Button) findViewById(R.id.pcmList);
        wavList = (Button) findViewById(R.id.wavList);
        pause.setVisibility(View.GONE);
        mActivity = this;
        audioRecorder = AudioRecorder.getInstance();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.start:
                if (Build.VERSION.SDK_INT >= 23) {
                    int checkRecordPermission = ContextCompat.checkSelfPermission(mActivity, Manifest.permission.RECORD_AUDIO);
                    int checkWritePermission = ContextCompat.checkSelfPermission(mActivity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                    if (checkRecordPermission != PackageManager.PERMISSION_GRANTED || checkWritePermission != PackageManager.PERMISSION_GRANTED) {
                        mActivity.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 125);
                        return;
                    } else {
                        startOrStop();
                    }
                } else {
                    startOrStop();
                }
                break;
            case R.id.pause:
                try {
                    if (audioRecorder.getStatus() == AudioRecorder.Status.STATUS_START) {
                        //暂停录音
                        audioRecorder.pauseRecord();
                        pause.setText("继续录音");
                        break;

                    } else {
                        audioRecorder.startRecord(null);
                        pause.setText("暂停录音");
                    }
                } catch (IllegalStateException e) {
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.pcmList:
                Intent showPcmList = new Intent(MainActivity.this, ListActivity.class);
                showPcmList.putExtra("type", "pcm");
                startActivity(showPcmList);
                break;

            case R.id.wavList:
                Intent showWavList = new Intent(MainActivity.this, ListActivity.class);
                showWavList.putExtra("type", "wav");
                startActivity(showWavList);
                break;
        }
    }

    private void startOrStop(){
        try {
            if (audioRecorder.getStatus() == AudioRecorder.Status.STATUS_NO_READY) {
                //初始化录音
                String fileName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
                audioRecorder.createDefaultAudio(fileName);
                audioRecorder.startRecord(null);
                start.setText("停止录音");
                pause.setVisibility(View.VISIBLE);
            } else {
                //停止录音
                audioRecorder.stopRecord();
                start.setText("开始录音");
                pause.setText("暂停录音");
                pause.setVisibility(View.GONE);
            }
        } catch (IllegalStateException e) {
            Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode){
            case 125:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    startOrStop();
                } else {
                    Toast.makeText(mActivity, "您已关闭录音需要时请到设置中打开录音权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (audioRecorder.getStatus() == AudioRecorder.Status.STATUS_START) {
            audioRecorder.pauseRecord();
            pause.setText("继续录音");
        }

    }

    @Override
    protected void onDestroy() {
        audioRecorder.release();
        super.onDestroy();

    }
}
